<template>
    <a-modal :keyboard="false" :maskClosable="false" title="陪护服务" :open="visible" :width="550" :confirmLoading="loading" centered @ok="handleSubmit" @cancel="handleCancel">
        <a-form layout="vertical" ref="formRef" :model="modelRef">
            <a-row :gutter="16" type="flex" justify="start">
                <a-col :xs="12" :sm="12" :md="24">
                    <a-form-item
                        label="陪护服务名称"
                        name="name"
                        :rules="[
                            {
                                required: true,
                                message: '请输入陪护服务名称',
                            },
                        ]"
                    >
                        <a-input v-model:value="modelRef.name" placeholder="请输入陪护服务名称"></a-input>
                    </a-form-item>
                </a-col>
                <a-col :xs="24" :sm="24" :md="24">
                    <a-form-item label="上传宣传图" v-bind="validateInfos.fileList">
                        <custom-upload v-model:fileList="modelRef.fileList" accept="image/*" :max-count="1" />
                        <template #extra>
                            <a-flex vertical>
                                <a-typography-text type="secondary">请参考图片上传比例为：160×144;</a-typography-text>
                                <a-typography-text type="secondary">请上传透明底图片;</a-typography-text>
                            </a-flex>
                        </template>
                    </a-form-item>
                </a-col>
                <a-col :xs="12" :sm="12" :md="12">
                    <a-form-item label="是否显示" v-bind="validateInfos.display">
                        <a-radio-group v-model:value="modelRef.display">
                            <a-radio :value="1">是</a-radio>
                            <a-radio :value="0">否</a-radio>
                        </a-radio-group>
                    </a-form-item>
                </a-col>
                <a-col :xs="12" :sm="12" :md="12">
                    <a-form-item label="是否居家" v-bind="validateInfos.home">
                        <a-radio-group v-model:value="modelRef.home">
                            <a-radio :value="1">是</a-radio>
                            <a-radio :value="0">否</a-radio>
                        </a-radio-group>
                    </a-form-item>
                </a-col>
                <a-col :span="12">
                    <a-form-item label="排序" v-bind="validateInfos.sort">
                        <a-input-number :step="1" :min="0" v-model:value="modelRef.sort" style="width: 100%" />
                    </a-form-item>
                </a-col>
            </a-row>
        </a-form>
    </a-modal>
</template>

<script lang="ts">
    import type { PropType } from 'vue';
    import { defineComponent, reactive, ref, watchEffect, watch, computed } from 'vue';
    import { Form, message } from 'ant-design-vue';
    import { cloneDeep, isUndefined } from 'lodash-es';
    import { getMedicalCaregiverType, createMedicalCaregiverType, updateMedicalCaregiverType } from '@/api/companion/cate';
    import { MinusCircleOutlined, PlusCircleOutlined } from '@ant-design/icons-vue';

    export default defineComponent({
        props: {
            visible: {
                type: Boolean,
                required: true,
            },
            model: {
                type: Object as PropType<any | null>,
                default: () => null,
            },
        },
        emits: ['cancel', 'success'],
        setup(props, { emit }) {
            const loading = ref(false);
            const formRef = ref<any>(null);
            const defaultForm = {
                projectCode: '',
                name: '',
                display: 1,
                home: 0,
                fileList: [],
                sort: 999
            };
            const modelRef = reactive<any>(cloneDeep(defaultForm));
            const rulesRef = reactive({
                name: [{ required: true, message: '请输入陪护服务名称' }],
                fileList: [{ required: true, message: '请上传上传宣传图', type: 'array' }],
                display: [{ required: true, message: '请选择是否显示' }],
            });
            const { validateInfos, resetFields, validate } = Form.useForm(modelRef, rulesRef);

            // 记录初始值
            const initValues = reactive({});
            watch(
                () => props.visible,
                () => {
                    Object.assign(initValues, cloneDeep(props.model));
                },
            );
            watchEffect(async () => {
                if (props.visible) {
                    if (props.model) {
                        Object.assign(modelRef, props.model);
                        console.log(props.model);

                        if (!isUndefined(modelRef.id)) {
                            getMedicalCaregiverType(modelRef.id).then(async res => {
                                const { advertisingImage, ...last } = res.data;
                                Object.assign(modelRef, last, {
                                    fileList: [
                                        {
                                            ...advertisingImage,
                                            url: advertisingImage.viewAddress,
                                        },
                                    ],
                                });
                            });
                        }
                    }
                }
            });

            const handleCancel = () => {
                formRef.value.resetFields();
                emit('cancel');
                Object.keys(modelRef).forEach(key => {
                    delete modelRef[key];
                });
                Object.assign(modelRef, cloneDeep(defaultForm));
            };

            const handleSubmit = async () => {
                formRef.value.validate().then(res => {
                    loading.value = true;
                    const { fileList = [], ...params } = modelRef;
                    if (fileList.length > 0) {
                        params.advertisingImage = fileList[0];
                    }

                    (isUndefined(modelRef.id) ? createMedicalCaregiverType : updateMedicalCaregiverType)(params)
                        .then(res => {
                            if (res.code == 200) {
                                message.success('操作成功');
                                emit('success');
                                handleCancel();
                            }
                        })
                        .finally(() => {
                            loading.value = false;
                        });
                });
            };
            return {
                loading,
                formRef,
                modelRef,
                handleSubmit,
                handleCancel,
                validateInfos,
                initValues,
            };
        },
        components: {
            MinusCircleOutlined,
            PlusCircleOutlined,
        },
    });
</script>
